﻿using System;
using System.Windows.Forms;
using SystemCustomers.DBUtils;

namespace SystemCustomers
{
    public partial class AddPriority : Form
    {
        public AddPriority()
        {
            InitializeComponent();
        }

        public string TypePriority
        {
            get { return txtbTypePriority.Text; }
            set { txtbTypePriority.Text = value; }
        }

        private bool IsValid()
        {
            return (txtbTypePriority.Text.Trim() != string.Empty);
        }

        private bool canAdd = false;

        private void EnableAdd()
        {
            btnAddPriority.Enabled = IsValid() && canAdd;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void btnAddPriority_Click(object sender, EventArgs e)
        {
            if (!SingletonManagerDb.Instance.DbInsertPriority(this.TypePriority)) return;
            MessageUtils.LogUtils.WriteToLog(" Add Priority: " + this.TypePriority);
            MessageUtils.LogUtils.SystemEventLogsInformation(" Add Priority: " + this.TypePriority);
            new MessageUtils.MessageBoxText(string.Format(" עדיפות נוסף בהצלחה: {0}", this.TypePriority)).OkMessaageBox();
            this.Close();
            GC.Collect();
        }

        private void txtbTypePriority_TextChanged(object sender, EventArgs e)
        {
            if (!MyRegEx.Validate(txtbTypePriority.Text, MyRegEx.OnlyCharsSpacesAndApostropheDigitsHebrew))
            {
                errorpPriority.SetError(txtbTypePriority, "רק אותיות גרש ורווחים מותרים");
                canAdd = false;
            }
            else
            {
                errorpPriority.SetError(txtbTypePriority, string.Empty);
                canAdd = true;
            }
            EnableAdd();
        }
    }
}
